﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Glucometer.ModuleHandler;

namespace Glucometer.Utility
{
    class Battery
    {
        public event d_EmptyArgument BatteryLevelDecreased;

        public event d_EmptyArgument BatteryLevelIncreased;

        public static int MAX_BATTERY_LEVEL = 4;

        public static int MIN_BATTERY_LEVEL = 0;

        //10 MINUTES MAX BATTERY LIFE
        public static double MAX_BATTERY_LIFE = 10 * 60 * 1000;

        //BATTERY CHARGING RATE
        public static double BATTERY_LIFE_CHARGE_RATE_PER_SECOND = 2000;

        //BATTERY DISCHARGING RATE
        public static double BATTERY_LIFE_DISCHARGE_RATE_PER_SECOND = 1000;

        //POLLING PER MINUTE
        public static int BATTERY_LIFE_POLLING_TIME_PERIOD = 1 * 1000;

        private System.Timers.Timer _batteryLifePollingTimer;

        public int BatteryLevel
        {
            get
            {
                double availbattpercent = (BatteryLife / MAX_BATTERY_LIFE) * 100;

                double levelrange = 100 / (MAX_BATTERY_LEVEL - MIN_BATTERY_LEVEL);

                if (availbattpercent == 0)
                    return MIN_BATTERY_LEVEL;
                else if (availbattpercent == 100)
                    return MAX_BATTERY_LEVEL;
                else
                    return (int)(availbattpercent / levelrange) + 1 + MIN_BATTERY_LEVEL;
            }
        }

        public double BatteryLife { get; set; }

        public Battery(double batteryLife)
        {
            this.BatteryLife = 0;

            if (batteryLife >= 0)
                this.BatteryLife = Math.Min(MAX_BATTERY_LIFE, batteryLife);

            _batteryLifePollingTimer = new System.Timers.Timer(BATTERY_LIFE_POLLING_TIME_PERIOD);
            _batteryLifePollingTimer.AutoReset = true;
            _batteryLifePollingTimer.Elapsed += new System.Timers.ElapsedEventHandler(_batteryLifePollingTimer_Elapsed);
            _batteryLifePollingTimer.Start();

            this.UseBattery = false;
            this.ChargeBattery = false;
        }

        public bool UseBattery { get; set; }

        public bool ChargeBattery { get; set; }

        void _batteryLifePollingTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //USED TIMEPERIOD IN SEC
            double timeperiod = ((System.Timers.Timer)sender).Interval / 1000;

            //KEEP TRACK OF LEVEL BEFORE CHANGING BATTERY LEVEL
            int oldbatterylevel = this.BatteryLevel;

            //IF BATTERY USED REDUCE LIFE
            if (UseBattery)
            {
                BatteryLife = Math.Max(0, BatteryLife -
                    (timeperiod * BATTERY_LIFE_DISCHARGE_RATE_PER_SECOND));
            }

            //IF BATTERY CHARGED INCREASE LIFE
            if (ChargeBattery)
            {
                BatteryLife = Math.Min(MAX_BATTERY_LIFE, BatteryLife +
                    (timeperiod * BATTERY_LIFE_CHARGE_RATE_PER_SECOND));
            }


            //CHECK IF THIS UPDATE CHANGED THE BATTERY LEVEL
            if (this.BatteryLevel > oldbatterylevel)
            {
                //THIS IS THE RISE IN BATTERY LEVEL
                //NOTIFY EVERYONE BATTERY LEVEL INCREASED
                if (BatteryLevelIncreased != null)
                    BatteryLevelIncreased();
            }

            else if (this.BatteryLevel < oldbatterylevel)
            {
                //THIS IS THE FALL IN BATTERY LEVEL
                //NOTIFY EVERYONE BATTERY LEVEL DECREASED
                if (BatteryLevelDecreased != null)
                    BatteryLevelDecreased();
            }
        }
    }
}
